package com.hengda.museumonline.utils;

import android.support.v4.widget.SwipeRefreshLayout;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hengda.museumonline.R;
import com.hengda.museumonline.ui.MainActivity;
import com.hengda.museumonline.view.MySwipeRefreshLayout;

/**
 * @author：JFWU on 2016/10/13 14:16
 * @E-mail：jeffy12138@126.com
 */
public class RefreshUtil {
    private static LinearLayout headView;
    private static int headerViewHeight;
    private static int downY;
    private static final int DOWN_PULL = 0;    // 头布局状态: 下拉刷新
    private static final int RELEASE_REFRESH = 1;    // 头布局状态:　释放刷新
    private static final int REFRESHING = 2;    // 头布局状态:　正在刷新中..
    private static int currentState = DOWN_PULL;
    private static TextView tvState;
    private static OnRefreshListener mOnRefreshListener;

    public static void onRefresh(MainActivity mainActivity, LinearLayout ll, final MySwipeRefreshLayout refreshLayout) {
        headView = (LinearLayout) View.inflate(mainActivity, R.layout.listview_header, null);
        tvState = (TextView) headView.findViewById(R.id.tv_listview_header_state);
        headView.measure(0, 0);    // 让系统框架去帮我们测量头布局的宽和高.
        // 获得一个测量后的高度, 只有在measure方法被调用完毕后才可以得到具体高度.
        headerViewHeight = headView.getMeasuredHeight();
        headView.setPadding(0, -headerViewHeight, 0, 0);
        ll.addView(headView, 0);
        refreshLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent ev) {
                switch (ev.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        // 当前的状态是否是正在刷新中, 如果是, 直接跳出.
                        if (currentState == REFRESHING) {
                            break;
                        }

                        int moveY = (int) ev.getY();

                        // 间距 = 移动y - 按下y;
                        int diffY = moveY - refreshLayout.downY;
                        // 计算头布局最新的paddingTop = -头布局高度 + 间距.
                        System.out.println("diffY:" + diffY);
                        int paddingTop = -headerViewHeight + diffY /3;
                        if (paddingTop > -headerViewHeight) {
                            if (paddingTop > 0 && currentState == DOWN_PULL) { // 头布局完全显示, 并且当前状态是下拉刷新, 进入到松开刷新的状态
                                currentState = RELEASE_REFRESH;
                                refreshHeaderViewState();
                            } else if (paddingTop < 0 && currentState == RELEASE_REFRESH) { // 头布局没有完全显示, 并且当前状态是松开刷新, 进入到下拉刷新的状态
                                currentState = DOWN_PULL;
                                refreshHeaderViewState();
                            }
                            headView.setPadding(0, paddingTop, 0, 0);
                            return true; // 自己处理用户触摸滑动的事件.
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        // 判断当前的状态是哪一种
                        if (currentState == DOWN_PULL) { // 当前是在下拉刷新状态下松开了, 什么都不做, 把头布局隐藏就可以.
                            headView.setPadding(0, -headerViewHeight, 0, 0);
                        } else if (currentState == RELEASE_REFRESH) { // 当前的状态属于释放刷新, 并且松开了. 应该把头布局正常显示, 进入正在刷新中状态.
                            headView.setPadding(0, 0, 0, 0);
                            currentState = REFRESHING;
                            refreshHeaderViewState();
                            // 调用用户的监听事件.
                            if (mOnRefreshListener != null) {
                                mOnRefreshListener.onPullDownRefresh();
                            }
                        }
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
    }

    /**
     * 根据当前的状态currentState来刷新头布局的状态.
     */
    private static void refreshHeaderViewState() {
        switch (currentState) {
            case DOWN_PULL:     // 下拉刷新
                //   ivArrow.startAnimation(downAnimation);
                tvState.setText("下拉刷新");
                break;
            case RELEASE_REFRESH: // 松开刷新
//                ivArrow.startAnimation(upAnimation);
                tvState.setText("松开刷新");
                break;
            case REFRESHING: // 正在刷新中
//                ivArrow.clearAnimation(); // 把自己身上的动画清除掉
//                ivArrow.setVisibility(View.INVISIBLE);
//                mProgressBar.setVisibility(View.VISIBLE);
                tvState.setText("正在刷新..");
                break;
            default:
                break;
        }
    }

    /**
     * 刷新完成, 用户调用此方法, 把对应的头布局或脚布局给隐藏掉
     */
    public static void onRefreshFinish() {

        headView.setPadding(0, -headerViewHeight + 1, 0, 0);
        currentState = DOWN_PULL;
        tvState.setText("下拉刷新");
    }

    public static void setOnRefreshListener(OnRefreshListener listener) {
        mOnRefreshListener = listener;
    }

    public interface OnRefreshListener {

        void onPullDownRefresh();

    }
}
